package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;
import com.shiwaixiangcun.core.repository.dictionary.DictionaryRepository;
import com.shiwaixiangcun.core.web.response.ResponseEntity;
import com.shiwaixiangcun.monitor.domain.Device;
import com.shiwaixiangcun.monitor.dto.DeviceListDto;
import com.shiwaixiangcun.monitor.dto.DeviceSearchDto;
import com.shiwaixiangcun.monitor.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.constraints.NotNull;

/**
 * Created by Administrator on 2018-01-15.
 */
@Controller
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DictionaryRepository dictionaryRepository;

    /**
     * 查询设备型号列表
     */
    @RequestMapping(value = "/device/model/list", method = RequestMethod.GET)
    public ResponseEntity getDeviceModelList() {
        return new ResponseEntity().success(dictionaryRepository.findByType(Device.MODEL_TYPE));
    }

    /**
     * 添加设备
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "deviceType", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "设备类型不能为空")),
            @ParameterConstraint(name = "deviceBrand", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "设备品牌不能为空")),
            @ParameterConstraint(name = "modelId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "设备型号ID不能为空")),
            @ParameterConstraint(name = "name", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "设备别名不能为空")),
            @ParameterConstraint(name = "number", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "设备编号不能为空"))
    })
    @RequestMapping(value = "/device/save", method = RequestMethod.POST)
    public String saveDevice(MonkeyDto monkeyDto) {
        deviceService.saveDevice(monkeyDto);
        return "SUCCESS:添加成功";
    }

    /**
     * 编辑设备
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "id", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "id不能为空"))
    })
    @RequestMapping(value = "/device/update", method = RequestMethod.PUT)
    public String updateDevice(MonkeyDto monkeyDto) {
        deviceService.updateDevice(monkeyDto);
        return "SUCCESS:更新成功";
    }

    /**
     * 设备详情
     */
    @RequestMapping(value = "/device/details", method = RequestMethod.GET)
    public ResponseEntity getDeviceDetails(Long id) {
        return new ResponseEntity().success(deviceService.getDeviceDetails(id));

    }

    /**
     * 查询设备分页列表
     */
    @RequestMapping(value = "/device/list", method = RequestMethod.GET)
    public Page<DeviceListDto> getDeviceList(Pageable pageable, DeviceSearchDto deviceSearchDto) {
        return deviceService.getDeviceList(pageable, deviceSearchDto);
    }

    /**
     * 查询接收器列表
     **/
    @RequestMapping(value = "/device/wirelessReceiver/list", method = RequestMethod.GET)
    public ResponseEntity getWirelessReceiverList() {
        return new ResponseEntity().success(deviceService.getWirelessReceiverList());
    }


}
